from rich.console import Console
from rich.table import Table
from rich import box


import pandas as pd
data = pd.read_csv("outputs/output.csv")
data = data.to_dict(orient="records")

def highlight_combination(text):
    # re查找字符串中用'包裹的内容
    import re
    pattern = re.compile(r"'(.*?)'")
    return pattern.sub(r"[bold red]\1[/bold red]", text)

# 统计分析
total_calls = len(data)
customer_service_negative = sum(1 for d in data if "不符合要求" in d["客服情绪评估"])
user_negative = sum(1 for d in data if "不满意" in d["用户情绪评估"])
issue_unresolved = sum(1 for d in data if "未得到解决" in d["用户问题解决评估"])
customer_service_and_user_negative = sum(1 for d in data if "不符合要求" in d["客服情绪评估"] and "不满意" in d["用户情绪评估"])
customer_service_negative_and_issue_unresolved = sum(1 for d in data if "不符合要求" in d["客服情绪评估"] and "未得到解决" in d["用户问题解决评估"])

# 是否规范
is_answer_process_standard = sum(1 for d in data if "不规范" in d["解答流程规范评估"])

# 使用 rich 打印结果
console = Console()

# 创建表格
table = Table(title="通话统计信息", box=box.MINIMAL_DOUBLE_HEAD, show_header=True, header_style="bold magenta")
table.add_column("统计项", style="cyan")
table.add_column("值", style="green")

# 添加数据到表格

table.add_row("总通话数", str(total_calls))
table.add_row("客服情绪不符合要求数", str(customer_service_negative))
table.add_row("客服情绪不符合要求占比", f"{customer_service_negative/total_calls:.2%}", style="bold red")
table.add_row("客户情绪异常数", str(user_negative))
table.add_row("客户情绪异常占比（全过程）", f"{user_negative/total_calls:.2%}")
table.add_row("问题未解决数", str(issue_unresolved))
table.add_row("问题未解决占比", f"{issue_unresolved/total_calls:.2%}")
table.add_row("解答流程不规范数", str(is_answer_process_standard))
table.add_row("解答流程不规范占比", f"{is_answer_process_standard/total_calls:.2%}")
table.add_row("客服异常且客户异常占比", f"{customer_service_and_user_negative/total_calls:.2%}")
table.add_row("客服异常且问题未解决占比", f"{customer_service_negative_and_issue_unresolved/total_calls:.2%}")
table.add_row("数据周期", f"2024-10-14")
table.add_row("平均数据处理时间", f"98.5s")

# 打印表格
console.print(table)
console.print(f"[red]注意：异常情绪包括但不限于：愤怒、焦虑、悲伤、厌恶等。[/red]")
console.print(f"[red]注意：客户情绪异常占比指的是用户全过程中是否有负面情绪，而并非最后结束时的情绪。[/red]")

# 打印客服异常数据样例
console.print("\n[bold red]客服异常数据样例：[/bold red]")
for entry in data:
    if "不符合要求" in entry["客服情绪评估"]:
        
        reason = entry['客服情绪原因']
        # 将 reson中用'包裹的内容高亮显示
        reason = highlight_combination(reason).replace('\n','')
        console.print(f"ID: {entry['ID']}, [yellow]原因[/yellow]: \n\t{reason}")

# 打印客服异常且问题未解决数据样例
console.print("\n[bold red]客服异常且问题未解决数据样例：[/bold red]")
for entry in data:
    if "不符合要求" in entry["客服情绪评估"] and "未得到解决" in entry["用户问题解决评估"]:
        reason = entry['用户问题解决原因']
        reason = highlight_combination(reason).replace('\n','')
        console.print(f"ID: {entry['ID']}, [yellow]判定为未解决的原因[/yellow]: \n\t{reason}")

# 打印用户异常、客服异常且问题未解决数据样例
console.print("\n[bold red]用户异常、客服异常且问题未解决数据样例：[/bold red]")
for entry in data:
    if "不满意" in entry["用户情绪评估"] and "不符合要求" in entry["客服情绪评估"] and "未得到解决" in entry["用户问题解决评估"]:
        reason = entry['用户问题解决原因']
        reason = highlight_combination(reason).replace('\n','')
        console.print(f"ID: {entry['ID']}, [yellow]判定为未解决的原因[/yellow]: \n\t{reason}")

# 打印客户异常数据样例
console.print("\n[bold red]客户异常数据样例：[/bold red]")
for entry in data:
    if "不满意" in entry["用户情绪评估"]:
        reason = entry['用户情绪原因']
        reason = highlight_combination(reason).replace('\n','')
        console.print(f"ID: {entry['ID']}, [yellow]原因[/yellow]: \n\t{reason}")